home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 23.1 KB | 705 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWContrH.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
-
- #include "FWFrameW.hpp"
-
- #ifndef FWCONTRH_H
- #include "FWContrH.h"
- #endif
-
- #ifndef FWSCLBAR_H
- #include "FWSclBar.h"
- #endif
-
- #ifndef FWPUSHBU_H
- #include "FWPushBu.h"
- #endif
-
- #ifndef FWRADIOB_H
- #include "FWRadioB.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWGRGLOB_H
- #include "FWGrGlob.h"
- #endif
-
- // ----- Foundation Layer -----
-
- #ifndef FWNOTIFN_H
- #include "FWNotifn.h"
- #endif
-
- #ifndef FWINTERE_H
- #include "FWIntere.h"
- #endif
-
- #ifndef FWBNDSTR_H
- #include "FWBndStr.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_ODFacet_xh
- #include <Facet.xh>
- #endif
-
- #ifndef SOM_ODCanvas_xh
- #include <Canvas.xh>
- #endif
-
- #ifndef SOM_ODTransform_xh
- #include <Trnsform.xh>
- #endif
-
- #ifndef SOM_ODShape_xh
- #include <Shape.xh>
- #endif
-
- //========================================================================================
- // Runtime Informations
- //========================================================================================
-
- #if FW_LIB_EXPORT_PRAGMAS
- #pragma lib_export on
- #endif
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwgadgts
- #endif
-
- FW_DEFINE_CLASS_M0(FW_CPrivMacControlHelper)
-
- //========================================================================================
- // Globales
- //========================================================================================
-
- WindowPtr FW_CPrivMacControlHelper::gScratchWindow;
-
- //========================================================================================
- // STRUCT FW_SPrivControlOwner
- //========================================================================================
- // SetupControl, RestoreControl need to be reentrant, so we keep a stack of control owners.
- // A new entry is placed on the stack everytime SetupControl is called and removed every-
- // time RestoreControl is called.
-
- struct FW_SPrivControlOwner
- {
- FW_SPrivControlOwner(WindowPtr window, FW_SPrivControlOwner* nextOwner) :
- fWindow(window), fNextOwner(nextOwner) { }
-
- WindowPtr fWindow;
- FW_SPrivControlOwner* fNextOwner;
- };
-
- //========================================================================================
- // CLASS FW_CPrivMacControlHelper
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::FW_CPrivMacControlHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacControlHelper::FW_CPrivMacControlHelper(Environment* ev,
- const FW_CClassInfo& classInfo,
- FW_CGadget* gadget) :
- fGadget(gadget),
- fControlHandle(NULL),
- fControlOwnerHead(NULL),
- fControlMargin(0, 0, 0, 0)
- {
- CheckForInitialize(ev, classInfo);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::~FW_CPrivMacControlHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacControlHelper::~FW_CPrivMacControlHelper()
- {
- if (fControlHandle)
- ::DisposeControl(fControlHandle);
-
- if (gScratchWindow)
- {
- long count;
- ::SetWRefCon(gScratchWindow, count = (::GetWRefCon(gScratchWindow) - 1));
- if (count == 0)
- {
- ::DisposeWindow(gScratchWindow);
- gScratchWindow = NULL;
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::CheckForInitialize
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::CheckForInitialize(Environment* ev, const FW_CClassInfo& classInfo)
- {
- if (classInfo == FW_TYPEID_FROM_POINTER(this))
- this->Initialize(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::Initialize
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::Initialize(Environment* ev)
- {
- if (gScratchWindow == NULL)
- {
- FW_SPlatformRect rBounds;
- ::SetRect(&rBounds, 0, 0, 10 , 10);
- gScratchWindow = ::NewCWindow(NULL,
- &rBounds,
- "\p",
- false,
- documentProc,
- NULL,
- false,
- 0);
- }
-
- ::SetWRefCon(gScratchWindow, ::GetWRefCon(gScratchWindow) + 1);
-
- FW_SPlatformPoint qdLocation;
- fGadget->GetLocation(ev).AsPlatformPoint(qdLocation);
- FW_SPlatformPoint qdSize;
- fGadget->GetSize(ev).AsPlatformPoint(qdSize);
-
- fControlHandle = CreateControlHandle(gScratchWindow, qdLocation, qdSize);
-
- ::SetControlReference(fControlHandle, (long) this);
-
- // ----- Remove it from the window -----
- ((WindowPeek) gScratchWindow)->controlList = NULL;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::ControlHandleToControlMgrHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacControlHelper *FW_CPrivMacControlHelper::ControlHandleToControlMgrHelper(ControlHandle handle)
- {
- return (FW_CPrivMacControlHelper *) ::GetControlReference(handle);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetControlMargin
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetControlMargin(const FW_SPlatformRect& controlMargin)
- {
- fControlMargin = controlMargin;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetBounds
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetBounds(const FW_SPlatformRect& controlBounds)
- {
- (*fControlHandle)->contrlRect = controlBounds;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetValue
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetValue(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size,
- short value)
- {
- SetupControl(window, location, size);
- ::SetControlValue(fControlHandle, value);
- RestoreControl();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetValue
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetValue(short value, FW_Boolean drawUsingCurrentPort)
- {
- if (drawUsingCurrentPort)
- ::SetControlValue(fControlHandle, value);
- else
- (*fControlHandle)->contrlValue = value;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetMin
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetMin(short min)
- {
- ::SetControlMinimum(fControlHandle, min);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetMax
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetMax(short max)
- {
- ::SetControlMaximum(fControlHandle, max);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetText
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetText(const FW_CString& text)
- {
- Str255 pascalText;
- text.ExportPascal(pascalText);
- ::SetControlTitle(fControlHandle, pascalText);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::GetText
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::GetText(FW_CString& text) const
- {
- Str255 pascalText;
- ::GetControlTitle(fControlHandle, pascalText);
- text.ReplaceAll(pascalText);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::Draw
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::Draw(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- SetupControl(window, location, size);
- ::Draw1Control(fControlHandle);
- RestoreControl();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::Show
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::Show(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- SetupControl(window, location, size);
- ::ShowControl(fControlHandle);
- RestoreControl();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::Hide
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::Hide(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- SetupControl(window, location, size);
- ::HideControl(fControlHandle);
- RestoreControl();
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::MouseDown
- //----------------------------------------------------------------------------------------
-
- Boolean FW_CPrivMacControlHelper::MouseDown(WindowPtr window,
- const FW_SPlatformPoint& whereMouse,
- const FW_SPlatformPoint& controlLocation,
- const FW_SPlatformPoint& controlSize)
- {
- SetupControl(window, controlLocation, controlSize);
- RestoreControl();
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::SetupControl
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::SetupControl(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- if (fControlHandle)
- {
- fControlOwnerHead = new FW_SPrivControlOwner((*fControlHandle)->contrlOwner,
- fControlOwnerHead);
- (*fControlHandle)->contrlOwner = window;
- (*fControlHandle)->contrlRect.top = location.v + fControlMargin.top;
- (*fControlHandle)->contrlRect.left = location.h + fControlMargin.left;
- (*fControlHandle)->contrlRect.bottom = location.v + size.v - fControlMargin.bottom;
- (*fControlHandle)->contrlRect.right = location.h + size.h - fControlMargin.right;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacControlHelper::RestoreControl
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacControlHelper::RestoreControl()
- {
- if (fControlHandle)
- {
- FW_SPrivControlOwner* saveToDelete = fControlOwnerHead;
- (*fControlHandle)->contrlOwner = fControlOwnerHead->fWindow;
- fControlOwnerHead = fControlOwnerHead->fNextOwner;
- delete saveToDelete;
- }
- }
-
- //========================================================================================
- // CLASS FW_CPrivMacScrollBarHelper
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(FW_CPrivMacScrollBarHelper, FW_CPrivMacControlHelper)
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::FW_CPrivMacScrollBarHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacScrollBarHelper::FW_CPrivMacScrollBarHelper(Environment* ev,
- const FW_CClassInfo& classInfo,
- FW_CScrollBar *scrollBar) :
- FW_CPrivMacControlHelper(ev, classInfo, scrollBar)
- {
- CheckForInitialize(ev, classInfo);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::~FW_CPrivMacScrollBarHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacScrollBarHelper::~FW_CPrivMacScrollBarHelper()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::MouseDown
- //----------------------------------------------------------------------------------------
-
- Boolean FW_CPrivMacScrollBarHelper::MouseDown(WindowPtr window,
- const FW_SPlatformPoint& whereMouse,
- const FW_SPlatformPoint& controlLocation,
- const FW_SPlatformPoint& controlSize)
- {
- SetupControl(window, controlLocation, controlSize);
-
- switch (::TestControl(GetControlHandle(), whereMouse))
- {
- case kControlDownButtonPart:
- case kControlUpButtonPart:
- case kControlPageDownPart:
- case kControlPageUpPart:
- {
- ControlActionUPP actionProc = NewControlActionProc(&CallActionProc);
- // Check and throw AMB
- ::TrackControl(GetControlHandle(), whereMouse, actionProc);
- DisposeRoutineDescriptor((UniversalProcPtr) actionProc);
- }
- break;
-
- case kControlIndicatorPart:
- {
- FW_CScrollBar* scrollBar = FW_DYNAMIC_CAST(FW_CScrollBar, GetGadget());
- FW_ASSERT(scrollBar);
-
- short before = GetValue();
- ::TrackControl(GetControlHandle(), whereMouse, NULL);
-
- FW_CFixed fxValue = FW_IntToFixed(GetValue());
- scrollBar->ScrollPositionChanged(somGetGlobalEnvironment(), fxValue, fxValue - FW_IntToFixed(before));
- }
- break;
- };
-
- RestoreControl();
-
- return TRUE;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::CreateControlHandle
- //----------------------------------------------------------------------------------------
-
- ControlHandle FW_CPrivMacScrollBarHelper::CreateControlHandle(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- FW_SPlatformRect rect(location.h, location.v,
- location.h + size.h, location.v + size.v);
-
- return ::NewControl(window, &rect, "\p", true,
- FW_CScrollBar::kDefaultScrollPos,
- FW_CScrollBar::kDefaultScrollMin,
- FW_CScrollBar::kDefaultScrollMax,
- scrollBarProc, 0);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::CallActionProc
- //----------------------------------------------------------------------------------------
-
- pascal void FW_CPrivMacScrollBarHelper::CallActionProc(ControlHandle controlHandle,
- short partCode)
- {
- FW_CPrivMacScrollBarHelper *scrollBarHelper =
- (FW_CPrivMacScrollBarHelper *) FW_CPrivMacControlHelper::ControlHandleToControlMgrHelper(controlHandle);
- // Check and throw AMB
- scrollBarHelper->ActionProc(partCode);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacScrollBarHelper::ActionProc
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacScrollBarHelper::ActionProc(short partCode)
- {
- Environment *ev = somGetGlobalEnvironment();
-
- FW_CScrollBar* scrollBar = FW_DYNAMIC_CAST(FW_CScrollBar, GetGadget());
- FW_ASSERT(scrollBar);
-
- FW_CFixed changeBy = FW_IntToFixed(0);
-
- switch (partCode)
- {
- case kControlDownButtonPart:
- changeBy = scrollBar->GetMinorScrollUnits(ev);
- break;
-
- case kControlUpButtonPart:
- changeBy = -scrollBar->GetMinorScrollUnits(ev);
- break;
-
- case kControlPageDownPart:
- changeBy = scrollBar->GetMajorScrollUnits(ev);
- break;
-
- case kControlPageUpPart:
- changeBy = -scrollBar->GetMajorScrollUnits(ev);
- break;
- };
-
- FW_CFixed oldValue = FW_IntToFixed(GetValue());
- FW_CFixed newValue = oldValue + changeBy;
-
- if (newValue < scrollBar->GetScrollMin(ev))
- newValue = scrollBar->GetScrollMin(ev);
- else if (newValue > scrollBar->GetScrollMax(ev))
- newValue = scrollBar->GetScrollMax(ev);
-
- SetValue(newValue.AsInt(), TRUE);
-
- scrollBar->ScrollPositionChanged(ev, newValue, newValue - oldValue);
- }
-
- //========================================================================================
- // CLASS FW_CPrivMacPushButtonHelper
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(FW_CPrivMacPushButtonHelper, FW_CPrivMacControlHelper)
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacPushButtonHelper::FW_CPrivMacPushButtonHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacPushButtonHelper::FW_CPrivMacPushButtonHelper(Environment* ev,
- const FW_CClassInfo& classInfo,
- FW_CPushButton* button) :
- FW_CPrivMacControlHelper(ev, classInfo, button)
- {
- CheckForInitialize(ev, classInfo);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacPushButtonHelper::~FW_CPrivMacPushButtonHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacPushButtonHelper::~FW_CPrivMacPushButtonHelper()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacPushButtonHelper::MouseDown
- //----------------------------------------------------------------------------------------
-
- Boolean FW_CPrivMacPushButtonHelper::MouseDown(WindowPtr window,
- const FW_SPlatformPoint& whereMouse,
- const FW_SPlatformPoint& controlLocation,
- const FW_SPlatformPoint& controlSize)
- {
- FW_CPushButton* button = FW_DYNAMIC_CAST(FW_CPushButton, GetGadget());
- FW_ASSERT(button);
-
- SetupControl(window, controlLocation, controlSize);
-
- // ---- Test first that we are really in the control -----
- short part = ::TestControl(GetControlHandle(), whereMouse);
-
- FW_Boolean inControl = (part == kControlButtonPart);
-
- if (inControl)
- {
- // ----- Now we can track -----
- if (::TrackControl(GetControlHandle(), whereMouse, NULL))
- button->ButtonPressed(somGetGlobalEnvironment());
- }
-
- RestoreControl();
-
- return inControl;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacPushButtonHelper::MakeDefaultButton
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacPushButtonHelper::MakeDefaultButton(FW_Boolean isDefault)
- {
- FW_CPushButton* button = FW_DYNAMIC_CAST(FW_CPushButton, GetGadget());
- FW_ASSERT(button);
-
- // Default button on the Mac to expand the size of the gadget and draw a
- // standard default button type frame
-
- FW_SPlatformRect margin;
- if (isDefault)
- margin.Set(4, 4, 4, 4);
- else
- margin.Set(0, 0, 0, 0);
-
- SetControlMargin(margin);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacPushButtonHelper::CreateControlHandle
- //----------------------------------------------------------------------------------------
-
- ControlHandle FW_CPrivMacPushButtonHelper::CreateControlHandle(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- // Should move this up to FW_CPrivMacControlHelper and add virtual method for getting
- // the control proc id. That's the only difference. [AMB]
-
- FW_SPlatformRect rect(location.h, location.v,
- location.h + size.h, location.v + size.v);
-
- return ::NewControl(window, &rect, "\p", true,
- FW_CScrollBar::kDefaultScrollPos,
- FW_CScrollBar::kDefaultScrollMin,
- FW_CScrollBar::kDefaultScrollMax,
- pushButProc, 0);
- }
-
- //========================================================================================
- // CLASS FW_CPrivMacRadioButtonHelper
- //========================================================================================
-
- FW_DEFINE_CLASS_M1(FW_CPrivMacRadioButtonHelper, FW_CPrivMacControlHelper)
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacRadioButtonHelper::FW_CPrivMacRadioButtonHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacRadioButtonHelper::FW_CPrivMacRadioButtonHelper(Environment* ev,
- const FW_CClassInfo& classInfo,
- FW_CRadioButton* button) :
- FW_CPrivMacControlHelper(ev, classInfo, button)
- {
- CheckForInitialize(ev, classInfo);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacRadioButtonHelper::~FW_CPrivMacRadioButtonHelper
- //----------------------------------------------------------------------------------------
-
- FW_CPrivMacRadioButtonHelper::~FW_CPrivMacRadioButtonHelper()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacRadioButtonHelper::MouseDown
- //----------------------------------------------------------------------------------------
-
- Boolean FW_CPrivMacRadioButtonHelper::MouseDown(WindowPtr window,
- const FW_SPlatformPoint& whereMouse,
- const FW_SPlatformPoint& controlLocation,
- const FW_SPlatformPoint& controlSize)
- {
- FW_CRadioButton* button = FW_DYNAMIC_CAST(FW_CRadioButton, GetGadget());
- FW_ASSERT(button);
-
- SetupControl(window, controlLocation, controlSize);
-
- // ---- Test first that we are really in the control -----
- FW_Boolean inControl = (::TestControl(GetControlHandle(), whereMouse) == kControlRadioButtonPart);
-
- if (inControl)
- {
- if (::TrackControl(GetControlHandle(), whereMouse, NULL))
- {
- SetValue(1, TRUE);
- button->ButtonPressed(somGetGlobalEnvironment());
- }
- }
-
- RestoreControl();
-
- return inControl;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacRadioButtonHelper::MakeDefaultButton
- //----------------------------------------------------------------------------------------
-
- void FW_CPrivMacRadioButtonHelper::MakeDefaultButton(FW_Boolean isDefault)
- {
- FW_CPushButton* button = FW_DYNAMIC_CAST(FW_CPushButton, GetGadget());
- FW_ASSERT(button);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CPrivMacRadioButtonHelper::CreateControlHandle
- //----------------------------------------------------------------------------------------
-
- ControlHandle FW_CPrivMacRadioButtonHelper::CreateControlHandle(WindowPtr window,
- const FW_SPlatformPoint& location,
- const FW_SPlatformPoint& size)
- {
- // Should move this up to FW_CPrivMacControlHelper and add virtual method for getting
- // the control proc id. That's the only difference. [AMB]
-
- FW_SPlatformRect rect(location.h, location.v,
- location.h + size.h, location.v + size.v);
-
- return ::NewControl(window, &rect, "\p", true,
- FW_CScrollBar::kDefaultScrollPos,
- FW_CScrollBar::kDefaultScrollMin,
- FW_CScrollBar::kDefaultScrollMax,
- radioButProc, 0);
- }
-
- #endif
-